package com.liy.cloud.base.rest;

import com.liy.cloud.base.entity.BaseLineEntity;
import com.liy.cloud.base.service.BaseLineService;
import com.liy.cloud.base.service.StationDeviceTreeService;
import com.liy.cloud.common.base.entity.BaseStationEntity;
import com.liy.cloud.common.base.service.BaseStationService;
import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.constants.PermissionCodes;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.web.controller.BaseCrudController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 车站 服务
 * @ClassName: BaseStationRest
 * @Description: 
 * @author shitou
 * @date 2018-07-04 20:01:58
 */
@RestController
@RequestMapping("/station")
@ActionPermission(code="BASE.STATION", description="车站管理")
public class BaseStationRest extends BaseCrudController<BaseStationService, BaseStationEntity>{

    @Autowired
    private StationDeviceTreeService stationDeviceTreeService;

    @Autowired
    private BaseLineService baseLineService;

    /**
     * 查询车站-设备树
     *
     * @param 
     * @return 
     * @auther libs
     * @date 2018/11/12 13:24
     */
    @ResponseBody
    @RequestMapping(value = {"/tree"}, method = {RequestMethod.POST})
    @MethodPermission(codes = PermissionCodes.SEARCH, description = "查询车站-设备树")
    public ResultVO tree(@RequestBody Map<String, Object> params) {
        ResultVO resultVO = new ResultVO();
        try{
            resultVO.setData(stationDeviceTreeService.getStationDeviceTree(params));
        }catch (Exception e){
            log.error("查询车站-设备树时出错：", e);
            resultVO.setSuccess(false);
            resultVO.setMessage("查询车站-设备树时出错：" + e.getMessage());
        }
        return resultVO;
    }
    
    /**
     * 查询 线路-车站 级联选择器
     *
     * @param 
     * @return 
     * @auther libs
     * @date 2019/9/2 15:08
     */
    @ResponseBody
    @RequestMapping(value = {"/cascader"}, method = {RequestMethod.GET})
    @MethodPermission(codes = PermissionCodes.SEARCH, description = "查询 线路-车站 级联选择器")
    public ResultVO cascader () {
        ResultVO resultVO = new ResultVO();
        try{
            List<Map<String, Object>> nodeList = new LinkedList<>();
            addNode(nodeList, "all-line", "全线网", null);
            resultVO.setData(nodeList);
            List<BaseLineEntity> lineEntities = baseLineService.findList(null);
            for (BaseLineEntity line: lineEntities) {
                List<Map<String, Object>> children = new LinkedList<>();
                addNode(children, "all-station", "全部", null);
                addNode(nodeList, line.getId(), line.getNameCn(), children);
                BaseStationEntity queryStation = new BaseStationEntity();
                queryStation.setLineId(line.getId());
                List<BaseStationEntity> stationEntities = baseService.findList(queryStation);
                for(BaseStationEntity station: stationEntities){
                    addNode(children, station.getId(), station.getNameCn(), null);
                }
            }
        }catch (Exception e){
            log.error("时出错：", e);
            resultVO.setSuccess(false);
            resultVO.setMessage("查询 线路-车站 级联选择器时出错：" + e.getMessage());
        }
        return resultVO;
    }

    private void addNode(List<Map<String, Object>> node, String value, String label, List<Map<String, Object>> children){
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("value", value);
        map.put("label", label);
        map.put("children", children);
        node.add(map);
    }

}      